package com.wjq.demo.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author wjq
 * @since 2023-03-13
 */
public class QuartzScheduler {

    private static final String NAME_OF_JOB = "Job1";
    private static final String NAME_OF_GROUP = "group1";
    private static final String NAME_OF_TRIGGER = "triggerStart";

    public static void main(String[] args) throws Exception {

        //show message to know about the main thread
        System.out.println(" The name of the QuartzScheduler main thread is: " + Thread.currentThread().getId());

        System.out.println(" The name of the QuartzScheduler main thread id is: " + Thread.currentThread().getId());
        //initialize scheduler instance from Quartz
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

        //start scheduler
        scheduler.start();

        //create scheduler trigger based on the time interval
        Trigger triggerNew =  createTrigger();

        JobDetail jobDetail = createJobDetail();
        //schedule trigger
        scheduler.scheduleJob(jobDetail,triggerNew);
    }
    private static JobDetail createJobDetail() {

        //create an instance of the JoDetails to connect Quartz job to the CreateQuartzJob
        return JobBuilder.newJob(SimpleJob.class).withIdentity(NAME_OF_JOB, NAME_OF_GROUP).build();

    }
    private static Trigger createTrigger() {

        //initialize time interval
        int TIME_INTERVAL = 1;

        //create a trigger to be returned from the method

        // triggerNew to schedule it in main() method
        return TriggerBuilder.newTrigger().withIdentity(NAME_OF_TRIGGER, NAME_OF_GROUP)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(TIME_INTERVAL).repeatForever())
                .build();
    }
}
